package com.xx.questionnaire.cis.UserOperation.login.servlet;

import java.io.IOException;
 








import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xx.questionnaire.cis.UserInfo.entity.User;
import com.xx.questionnaire.cis.UserOperation.login.service.LoginIService;
import com.xx.questionnaire.cis.UserOperation.login.service.LoginService;

 

 
/**
 * 获取登陆信息并交由登陆模块
 * @author Administrator
 *
 */
public class LoginServlet extends HttpServlet {
	User user=null;
	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.service(arg0, arg1);
		System.out.println("调用方法"+Thread.currentThread().getStackTrace()[1].getMethodName());
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public LoginServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		/*response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();*/
		doPost(request, response);
	}

	 
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			String UserName=null;
			String PassWord=null;
			UserName = request.getParameter("username");
			PassWord = request.getParameter("password");
			user = new User(UserName, PassWord);
			System.out.println(UserName+": " + PassWord);
		  LoginIService loginS = new LoginService();
			 
		 boolean stateBl=	loginS.loginState(user);
			 
			if(stateBl==true){
			request.setAttribute("message","login success!");
				
			}else{
				request.setAttribute("message", "sorry,login Failure!");
			}
		  request.getRequestDispatcher("/index.jsp").forward(request, response);
	}

	/**
	 * Returns information about the servlet, such as 
	 * author, version, and copyright. 
	 *
	 * @return String information about this servlet
	 */
	public String getServletInfo() {
		return "This is my default servlet created by Eclipse";
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
